package org.cloudie.docs.editor.client.content.dialogs;

import org.cloudie.docs.editor.client.commands.generic.WindowAddCommand;
import org.cloudie.docs.editor.client.events.CommandEvent;
import org.cloudie.docs.editor.client.windows.GenericWindow.WindowContents;
import org.cloudie.docs.editor.client.windows.GenericWindow.WindowContents.ContentType;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

/**
 * A dialog window containing a New Window form.
 */
public class NewWindowDialog extends FormDialog {

  protected static NewWindowDialog instance;
  
  public static NewWindowDialog get() {
    if (instance == null) {
      instance = new NewWindowDialog();
    }
    return instance;
  }
  
  private TextBox windowAction, windowTitle;
  private TextArea windowContents, windowGadgetCode;
  private RadioButton isGadget, isStatic, isFrame;
  private Anchor gadgetsLink;
  
  /**
   * Constructs a New Window dialog.
   */
  protected NewWindowDialog() {
    super("New Desktop Window", true);
    addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
      }
    });
    buildForm();
  }
  
  /**
   * Builds the New Window form.
   */
  @Override
  protected void buildForm() {
    isGadget = new RadioButton("Type", "Gadget");
    isGadget.setValue(true);
    isGadget.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
      public void onValueChange(ValueChangeEvent<Boolean> event) {
        content.getRowFormatter().setVisible(2, false);
        content.getRowFormatter().setVisible(3, false);
        content.getRowFormatter().setVisible(4, true);
        content.getRowFormatter().setVisible(5, true);
      }
    });
    isStatic = new RadioButton("Type", "Static Content");
    isStatic.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
      public void onValueChange(ValueChangeEvent<Boolean> event) {
        content.getRowFormatter().setVisible(2, false);
        content.getRowFormatter().setVisible(3, true);
        content.getRowFormatter().setVisible(4, false);
        content.getRowFormatter().setVisible(5, false);
      }
    });
    isFrame = new RadioButton("Type", "Frame");
    isFrame.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
      public void onValueChange(ValueChangeEvent<Boolean> event) {
        content.getRowFormatter().setVisible(2, true);
        content.getRowFormatter().setVisible(3, false);
        content.getRowFormatter().setVisible(4, false);
        content.getRowFormatter().setVisible(5, false);
      }
    });
    windowTitle = new TextBox();
    windowTitle.setWidth("300px");
    windowAction = new TextBox();
    windowAction.setWidth("300px");
    gadgetsLink = new Anchor();
    gadgetsLink.setHref("http://www.google.com/ig/directory?synd=open");
    gadgetsLink.setText("Browse all gadgets");
    gadgetsLink.setTarget("_blank");
    windowContents = new TextArea();
    windowContents.setSize("300px", "80px");
    windowGadgetCode = new TextArea();
    windowGadgetCode.setSize("300px", "80px");
    submit.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
          if (windowTitle.getValue() == null || windowTitle.getValue().equals("")) {
        	Window.alert("Title is a required field.");
        	return;
          }
          if (isGadget.getValue()) {
            if (windowGadgetCode.getValue() == null || windowGadgetCode.getValue().equals("")) {
              Window.alert("Gadget code is a required field.");
              return;
            }
        	WindowAddCommand cmd = new WindowAddCommand(null, windowTitle.getValue(),
        	    new WindowContents(ContentType.GADGET, windowGadgetCode.getValue()),
        	    0, 0, 200, 100);
            CommandEvent.fire(cmd);
          } else if (isStatic.getValue()) {
        	WindowAddCommand cmd = new WindowAddCommand(null, windowTitle.getValue(),
        	    new WindowContents(ContentType.STATIC, windowContents.getValue()),
        	    0, 0, 200, 100);
            CommandEvent.fire(cmd);
          } else {
            if (windowAction.getValue() == null || windowAction.getValue().equals("")) {
              Window.alert("Content URL is a required field.");
              return;
            }
          	WindowAddCommand cmd = new WindowAddCommand(null, windowTitle.getValue(),
        	    new WindowContents(ContentType.FRAME, windowAction.getValue()),
        	    0, 0, 200, 100);
            CommandEvent.fire(cmd);
          }
          hide();
  	  }
    });
    cancel.addClickHandler(new ClickHandler(){
	  public void onClick(ClickEvent event) {
		  hide();
	  }
    });
  	HorizontalPanel rbl = new HorizontalPanel();
    rbl.add(isGadget);
  	rbl.add(isStatic);
  	rbl.add(isFrame);
    addField(rbl, "Type:");
    addField(windowTitle, "Title:");
    addField(windowAction, "Content URL:");
    addField(windowContents, "Contents:");
    addField(windowGadgetCode, "Gadget Code:");
    addField(gadgetsLink, "");
    content.getRowFormatter().setVisible(2, false);
    content.getRowFormatter().setVisible(3, false);
    super.buildForm();
  }
  
  @Override
  public void hide() {
    super.hide();
    resetForm();
  }
  
  @Override
  public void resetForm() {
    isGadget.setValue(true);
    isStatic.setValue(false);
    isFrame.setValue(false);
    content.getRowFormatter().setVisible(2, false);
    content.getRowFormatter().setVisible(3, false);
    content.getRowFormatter().setVisible(4, true);
    content.getRowFormatter().setVisible(5, true);
    windowTitle.setValue("");
    windowAction.setValue("");
    windowContents.setValue("");
    windowGadgetCode.setValue("");
  }
  
}
